﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    /// <summary>
    /// This class holds information on a login process
    /// </summary>
    public class MM_Login_Process
    {
        #region Variable declarations
        /// <summary>The error from the process, if any</summary>
        public Exception Err;

        /// <summary>The current step on our task</summary>
        public int TaskCurrent = 0;

        /// <summary>The total number of steps for our task</summary>
        public int TaskTotal = 0;

        /// <summary>Our delegate to handle our login process</summary>
        /// <returns></returns>
        public delegate bool LoginProcessDelegate(MM_Login_Process Process, out Exception ex);

        /// <summary>The method to be called for logging in</summary>
        public LoginProcessDelegate Method;

        /// <summary>The title of our process</summary>
        public String Title;
        #endregion

        #region Initialization
        /// <summary>
        /// Add a new process 
        /// </summary>
        /// <param name="LoginProcesses"></param>
        /// <param name="Title"></param>
        /// <param name="Method"></param>
        public static void AddProcess(Dictionary<string, MM_Login_Process> LoginProcesses, String Title, LoginProcessDelegate Method)
        {
            MM_Login_Process NewProcess = new MM_Login_Process(Title, Method);
            LoginProcesses.Add(Title, NewProcess);
        }


        /// <summary>
        /// Initialize a new login process
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Method"></param>
        public MM_Login_Process(String Title, LoginProcessDelegate Method)
        {
            this.Title = Title;
            this.Method = Method;
        }
        #endregion

        #region Execution
        /// <summary>
        /// Begin invoking our method
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public IAsyncResult BeginInvoke(out Exception ex)
        {
            return Method.BeginInvoke(this, out ex,null,null);
        }

        /// <summary>
        /// End our invocation
        /// </summary>
        /// <param name="Result"></param>
        /// <param name="ex"></param>
        /// <returns></returns>
        public bool EndInvoke(IAsyncResult Result, out Exception ex)
        {            
            return Method.EndInvoke(out ex, Result);
        }

        /// <summary>
        /// Invoke our login process
        /// </summary>
        /// <returns></returns>
        public Exception Invoke()
        {
            Exception ex;            
            Method(this, out ex);
            return ex;
        }
        #endregion
    }
}
